package cn.stylefeng.guns.modular.system.service;

import cn.stylefeng.guns.core.common.exception.BizExceptionEnum;
import cn.stylefeng.guns.core.common.page.LayuiPageFactory;
import cn.stylefeng.guns.core.util.RandomString;
import cn.stylefeng.guns.modular.system.entity.Coupon;
import cn.stylefeng.guns.modular.system.mapper.CouponMapper;
import cn.stylefeng.guns.modular.system.model.CouponDto;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.RequestEmptyException;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.*;

@Log4j2
@Service
public class CouponService extends ServiceImpl<CouponMapper, Coupon> {

    /**
     * 生成兑换码
     *
     * @param length
     * @param count
     */
    public void generateCoupon(Integer length, Integer count) {
        Set<Coupon> set = RandomString.getSN(length, count);
        try {
            this.saveBatch(set);
        } catch (Exception e) {
            log.info(e);
            generateCoupon(length, count);
        }
    }

    /**
     * 条件查询 分页
     *
     * @param status
     * @param beginTime
     * @param endTime
     * @param condition
     * @return
     */
    public Page<Map<String, Object>> selectCoupons(String status, String beginTime, String endTime,
                                                   String condition) {
        Page page = LayuiPageFactory.defaultPage();
        return this.baseMapper.selectCoupons(page, status, beginTime, endTime, condition);
    }

    /**
     * 添加兑换劵
     */
    public void addCoupon(CouponDto couponDto) {
        Coupon coupon = new Coupon();
        BeanUtils.copyProperties(couponDto, coupon);
        coupon.setExportTime(new Date());
        coupon.setStatus("2");
        try {
            baseMapper.insert(coupon);
        } catch (Exception e) {
            throw new ServiceException(BizExceptionEnum.REQUEST_INVALIDATE);
        }
    }

    public void editCoupon(CouponDto couponDto) {
        if (ToolUtil.isOneEmpty(couponDto, couponDto.getId())) {
            throw new RequestEmptyException();
        }
        Coupon old = this.getById(couponDto.getId());
        BeanUtils.copyProperties(couponDto, old);
        old.setStatus("2");
        this.updateById(old);
    }

    public void exportExcel(Long[] ids) {
        List<Coupon> coupons = (List<Coupon>) this.listByIds(Arrays.asList(ids));
        for (Coupon coupon : coupons) {
            coupon.setExportTime(new Date());
            coupon.setStatus("1");

        }
        updateBatchById(coupons);
    }

    public void couponFillOut(CouponDto couponDto) {
        Coupon coupon = this.getOne(new QueryWrapper<Coupon>()
                .eq("coupon_sn", couponDto.getCouponSN())
        );

        if (coupon != null && !coupon.getStatus().equals("1")) {//存在验证码 或者 不是导出状态
            throw new ServiceException(BizExceptionEnum.COUPON_SN_ERROR);
        } else {
            BeanUtils.copyProperties(couponDto, coupon,"id");
            coupon.setStatus("2");
            this.updateById(coupon);
        }

    }

    public Coupon selectBySnAndphone(String couponSN, String phone) {
        Coupon coupon = this.getOne(new QueryWrapper<Coupon>()
                .eq("coupon_sn", couponSN)
                .eq("phone", phone)
        );

        if (coupon != null) {
            throw new ServiceException(BizExceptionEnum.COUPON_SNPHONE_ERROR);
        } else {
            return coupon;
        }
    }
}
